package com.rm.zeyou.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.rm.zeyou.entity.User;
import com.rm.zeyou.entity.UserAddress;
import com.rm.zeyou.response.ApiRestResponse;
import com.rm.zeyou.service.UserAddressService;
import com.rm.zeyou.util.login.LoginUtil;
import com.rm.zeyou.vo.request.AddAddressVo;
import com.rm.zeyou.vo.request.GetAddressVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 用户地址表 前端控制器
 * </p>
 *
 * @author YanYang
 * @since 2025-03-12 15:55:09
 */
@Api(tags = "用户地址相关")
@RestController
@RequestMapping("/api/userAddress")
public class UserAddressController {
    @Resource
    private LoginUtil loginUtil;
    @Resource
    private UserAddressService userAddressService;


    @ApiOperation(value = "用户添加地址")
    @SaCheckLogin(type = "user")
    @PostMapping("/addAddress")
    public ApiRestResponse<Integer> addAddress(@RequestBody AddAddressVo addVo) {
        User user = loginUtil.getUser();
        return userAddressService.addAddress(user.getId(),addVo);
    }

    @ApiOperation(value = "用户删除地址")
    @GetMapping("/deleteAddress")
    @SaCheckLogin(type = "user")
    public ApiRestResponse<Integer> userDeleteAddress( @RequestParam Integer id) {
        loginUtil.getUser();
        return userAddressService.deleteAddress(id);
    }

    @ApiOperation(value = "用户更新地址")
    @SaCheckLogin(type = "user")
    @PostMapping("/updateAddress")
    public ApiRestResponse<Integer> updateAddress(@RequestBody AddAddressVo updateVo) {
        User user = loginUtil.getUser();
        return userAddressService.updateAddress(user.getId(),updateVo);
    }

    @ApiOperation(value = "查询用户地址列表")
    @GetMapping("/selectAddressList")
    @SaCheckLogin(type = "user")
    public ApiRestResponse<List<UserAddress>> selectAddressList() {
        User user = loginUtil.getUser();
        return ApiRestResponse.success(userAddressService.list(new LambdaQueryWrapper<UserAddress>().eq(UserAddress::getUserId, user.getId()).orderByDesc(UserAddress::getCreateTime)));
    }

    @ApiOperation(value = "根据经纬度获取地址")
    @PostMapping("/getAddress")
    public ApiRestResponse<String> getAddress(@RequestBody GetAddressVo requestVo) {
        return userAddressService.getAddress(requestVo);
    }

    @ApiOperation(value = "首页位置")
    @PostMapping("/indexAddress")
    public ApiRestResponse<String> indexAddress(@RequestBody GetAddressVo requestVo) {
        return userAddressService.indexAddress(requestVo);
    }

    @ApiOperation(value = "技师页定位")
    @PostMapping("/technicianAddress")
    public ApiRestResponse<String> technicianAddress(@RequestBody GetAddressVo requestVo) {
        return userAddressService.technicianAddress(requestVo);
    }

}
